package cn.com.huzo.webapp.controller.tech;

import cn.com.huzo.model.entities.tech.MrpFeedingHeaderEntity;
import cn.com.huzo.model.entities.tech.MrpFeedingLinesEntity;
import cn.com.huzo.service.master.MrpFeedingTypeManager;
import huzofw.common.web.controller.BaseFormController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import cn.com.huzo.service.tech.MrpFeedingHeaderManager;
import org.apache.commons.collections4.MapUtils;
import cn.com.huzo.service.tech.MrpFeedingLinesManager;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * 补料单画面控制器
 *
 * @author 吴旭
 */
@Controller
public class MrpFeedingController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">    
    @Resource(name = "MrpFeedingHeaderManager")
    private MrpFeedingHeaderManager _manager_header;

    @Resource(name = "MrpFeedingLinesManager")
    private MrpFeedingLinesManager _manager_lines;

    @Resource(name = "MrpFeedingTypeManager")
    private MrpFeedingTypeManager _manager_mrp_feeding_type;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到补料单一览画面（主辅料）
     * <p>
     * @param request
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("tech/mrp/feeding/list.html")
    public ModelAndView redirectListWnd(HttpServletRequest request) throws Exception {
        ModelAndView view = new ModelAndView();
        view.setViewName("tech.mrp_feeding.list.def");
        return view;
    }

    /**
     * 跳转物料需求计划（补料单）编辑画面
     * <p>
     * @param action 操作类别 1：增加 2：编辑
     * @param mrpId 物料需求计划标识
     * @return
     * @throws java.lang.Exception
     */
    @RequestMapping("tech/mrp/feeding/{action}/{mrpId}/edit.html")
    public ModelAndView redirectEditWnd(@PathVariable(value = "action") int action,
            @PathVariable(value = "mrpId") Long mrpId) throws Exception {
        ModelAndView view = new ModelAndView();

        // 为画面设置变量
        view.addObject("MrpFeedingTypeList", _manager_mrp_feeding_type.fetchAll());  // 设置补料类型
        view.addObject("action", action);
        view.addObject("mrpId", mrpId);
        view.setViewName("tech.mrp_feeding.edit.def");
        return view;
    }

    /**
     * 弹出补料单选择画面，供采购订单调用
     * <p>
     * @return
     */
    @RequestMapping("tech/mrp_feeding_for_import/choice.html")
    public ModelAndView redirectMrpFeedingChoiceForImport() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.tech.mrp_feeding_for_import.choice.def");
        return view;
    }

    /**
     * 弹出补料单选择画面，供采购订单调用
     * <p>
     * @return
     */
    @RequestMapping("tech/mrp_feeding_for_po/choice.html")
    public ModelAndView redirectMrpFeedingChoiceForPo() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.tech.mrp_feeding_for_po.choice.def");
        return view;
    }

    /**
     * 补料单打印预览画面
     * <p>
     * @return
     */
    @RequestMapping("tech/mrp/feeding/print.html")
    public ModelAndView redirectPrintPreviewWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("print.tech.mrp_feeding.print.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="主表-数据处理部分">
    /**
     * 得到新的单据号
     * <p>
     * @return @throws Exception
     */
    @RequestMapping(value = "tech/mrp/feeding/header/fetchNewBillNo.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchNewBillNo() throws Exception {
        Map<String, Object> rtnVal = new HashMap();
        rtnVal.put("BillNo", _manager_header.fetchNewBillNo());
        return rtnVal;
    }

    /**
     * 根据标识取得补料单数据。
     *
     * @param criterion 补料单标识及审核人（登录者）标识
     * @return 补料单数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/mrp/feeding/fetchByKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchByHeaderKey(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchByHeaderKey(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getLong(criterion, "CurrentUserId"),
                MapUtils.getLong(criterion, "WorkFlowId"));
    }

    /**
     * 根据条件取得补料单信息分页数据。
     * <p>
     * @param criterion 检索条件
     * @return 补料单信息分页数据
     * @throws Exception
     */
    @RequestMapping(value = "tech/mrp/feeding/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchForPage(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchForPage(criterion);
    }

    /**
     * 保存补料单信息数据。
     * <p>
     * @param instance 要保存的补料单信息对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "tech/mrp/feeding/header/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody final MrpFeedingHeaderEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getHeaderId() > 0) { // 编辑
            rowsAffected = (long) _manager_header.update(instance);
        } else {                          // 新增
            rowsAffected = _manager_header.insert(instance);
        }

        return rowsAffected;
    }

    /**
     * 删除补料单信息数据。
     *
     * @param criterion 要删除的数据
     * @return 删除后的数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/mrp/feeding/header/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> delete(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.delete(criterion);
    }

    /**
     * 设置采购完成状态。
     *
     * @param criterion 物料需求计划标识及审核人（登录者）标识
     * @return 物料需求计划数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/mrp/feeding/setPoOverFlag.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> setPoOverFlag(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_header.setPoOverFlag(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getLong(criterion, "CurrentUserId"),
                MapUtils.getLong(criterion, "WorkFlowId"),
                MapUtils.getShort(criterion, "PoOverFlag"));
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="明细-数据处理部分">
    /**
     * 根据标识取得补料单数据。
     *
     * @param headerKey 补料单标识
     * @return 补料单数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/mrp/feeding/lines/fetchLinesByHeaderKey.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<MrpFeedingLinesEntity> fetchLinesByHeaderKey(@RequestBody Long headerKey) throws Exception {
        return _manager_lines.fetchLinesByHeaderKey(headerKey);
    }

    /**
     * 用于采购订单引入补料单明细
     *
     * @param criterion 成品标识、调用来源
     * @return 物料需求计划明细数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/mrp/feeding/lines/fetchForPage.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchLinesForPage(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.fetchForPage(criterion);
    }

    /**
     * 插入补料单明细数据。
     * <p>
     * @param itemList 要保存的数据。
     * @return 影响行数.
     * @throws Exception
     */
    @RequestMapping(value = "tech/mrp/feeding/lines/insert.html",
            method = RequestMethod.POST)

    public @ResponseBody
    List<MrpFeedingLinesEntity> insertLines(@RequestBody final List<MrpFeedingLinesEntity> itemList) throws Exception {
        return _manager_lines.insertLines(itemList);
    }

    /**
     * 更新补料单明细数据。
     * <p>
     * @param itemList 要保存的数据。
     * @return 影响行数.
     * @throws Exception
     */
    @RequestMapping(value = "tech/mrp/feeding/lines/update.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> updateLines(@RequestBody final List<MrpFeedingLinesEntity> itemList) throws Exception {
        if (itemList != null && !itemList.isEmpty()) {
            return _manager_lines.batchUpdate(itemList);
        } else {
            return new HashMap();
        }
    }

    /**
     * 根据料号更该明细材料数据。
     *
     * @param criterion 要更新的数据（行标识、料号）
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/mrp/feeding/lines/updateLinesByMaterialId.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> updateLinesByMaterialId(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.updateLinesByMaterialId(criterion);
    }

    /**
     * 引入类似的补料单明细数据。
     * <p>
     * @param criterion 引入条件
     * @return 影响行数.
     * @throws Exception
     */
    @RequestMapping(value = "tech/mrp/feeding/lines/import.html",
            method = RequestMethod.POST)

    public @ResponseBody
    List<MrpFeedingLinesEntity> importLinesFromOtherBill(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.importLinesFromOtherBill(
                MapUtils.getLong(criterion, "HeaderId"),
                MapUtils.getLong(criterion, "ImportedBillHeaderId"),
                MapUtils.getInteger(criterion, "MaxOrderNo"),
                MapUtils.getLong(criterion, "UpdatedBy"));
    }

    /**
     * 从剪切板粘贴数据。
     *
     * @param criterion 剪切板数据
     * @return 粘贴后的补料明细。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/mrp/feeding/lines/paste.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<MrpFeedingLinesEntity> insertFromClipboard(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.insertFromClipboard(criterion);
    }

    /**
     * 删除补料单明细数据。
     *
     * @param criterion 要删除的明细标识、主表id、删除的明细信息等
     * @return 删除后的数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/mrp/feeding/lines/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteLines(@RequestBody final Map<String, Object> criterion) throws Exception {
        return _manager_lines.delete(criterion);
    }

    /**
     * 批量删除补料单明细数据。
     *
     * @param itemList 要删除的补料单明细对象
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/mrp/feeding/lines/batchDelete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    List<MrpFeedingLinesEntity> batchDeleteLines(@RequestBody final List<MrpFeedingLinesEntity> itemList) throws Exception {
        return _manager_lines.batchDeleteLines(itemList);
    }

    /**
     * 更新补料单明细排列顺序。
     *
     * @param itemList 要更新的明细列表
     * @return
     *
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "/tech/mrp/feeding/lines/updateLinesRowNo.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer updateLinesRowNo(@RequestBody final List<MrpFeedingLinesEntity> itemList) throws Exception {
        return _manager_lines.updateLinesRowNo(itemList);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="打印处理部分">
    /**
     * 打印预览。
     *
     * @param criterion 标识等
     * @return 物料需求计划数据。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "tech/mrp/feeding/fetchPrintPreviewData.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> fetchPrintPreviewData(@RequestBody Map<String, Object> criterion) throws Exception {
        return _manager_header.fetchPrintPreviewData(criterion);
    }
    // </editor-fold>
}
